package com.xiaoyu.base.op;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bytedance.applog.AppLog;
import com.bytedance.applog.InitConfig;
import com.bytedance.applog.UriConfig;
import com.bytedance.applog.encryptor.EncryptorUtil;

import com.xiaoyu.base.AppConfig;
import com.xiaoyu.base.BuildConfig;

import in.srain.cube.util.DataTrack;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import in.srain.cube.util.CLog;

public class DataTrackImpl implements DataTrack {

    private static final String TAG = DataTrackImpl.class.getSimpleName();
    private static final String DATA_RANGER_REPORT_URL = "https://datarangers-event.myrightone.com";
    private String mChannel = "";
    private Bundle superProperties = new Bundle();

    public DataTrackImpl() {
    }

    public void onApplicationCreate(String channel, Context context) {
        mChannel = channel;

        initDataRanger(context);
        logEvent("app_start");
    }

    private void initDataRanger(Context context) {
        UriConfig uriConfig = UriConfig.createByDomain(DATA_RANGER_REPORT_URL, null);

        InitConfig config = new InitConfig(AppConfig.dataRangerAppId(), mChannel)
                .setUriConfig(uriConfig)
                .setLogger(BuildConfig.DEBUG ? (msg, t) -> CLog.d(TAG, msg, t) : null)
                .setEncryptor(EncryptorUtil::encrypt)
                .setAutoStart(true);
        config.setAbEnable(true);

        AppLog.init(context, config);
    }

    private void logEvent(@NonNull String name) {
        logEvent(name, new Bundle());
    }

    public void logEvent(@NonNull String name, @NonNull Bundle properties) {
        CLog.d(TAG, "log event: name: %s,  properties: %s", name, properties);
        logEventForDataRanger(name, properties);
    }

    private void logEventForDataRanger(@NonNull String name, Bundle properties) {
        Set<String> keys = properties.keySet();
        for (String key : keys) {
            Object o = properties.get(key);
            if (o instanceof Boolean) {
                properties.putString(key, String.valueOf(o));
            } else if (o instanceof List) {
                properties.putString(key, TextUtils.join(",", (List) o));
            }
        }
        properties.putAll(superProperties);
        AppLog.onEventV3(name, properties);
    }

    public void setUserId(String uid) {
        if (!TextUtils.isEmpty(uid)) {
            AppLog.setUserUniqueID(uid);
        }
    }

    public void setUserProperty(@NonNull String property, @Nullable String value) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(property, value);
        AppLog.setHeaderInfo(map);
    }

    public <T> T getABTestValue(@NonNull String property, T def) {
        return AppLog.getAbConfig(property, def);
    }

    @Override
    public void setSuperProperty(@NonNull String property, @Nullable String value) {

        superProperties.putString(property, value);
    }

    @Override
    public String getDeviceId(String platform) {
        return AppLog.getDid();
    }

    public void flushLogEvent() {
        AppLog.flush();
    }
}